
package org.vectrics.user.dataweb;

import org.vectrics.common.dom.MarshalUtils;
import org.vectrics.SystemException;
import org.vectrics.VectricsException;
import org.apache.log4j.Logger;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;
import org.vectrics.user.PersonService;
import org.vectrics.user.ContactInfo;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;




public class FindUserDetailsAction extends UserWebAction {
	private transient static Logger log = Logger.getLogger(FindUserDetailsAction.class);
	
	/**
	 * 
	 */
	public FindUserDetailsAction() {
		super();
	}


	public void getData(String command, Element requestData, 
				Element dataElement) {
					
		String userId = null;
		log.debug("getData() - put user details in document");
		NodeList userNodes = requestData.getElementsByTagName("user");
		if (userNodes.getLength() > 0) {
			Element userElement = (Element)userNodes.item(0); 
			userId = userElement.getAttribute("id");
		}
		else {
			throw new SystemException("Request element did not have a 'user' child element");
		}

		log.debug("getData() - found user element, finding user with passed id: " + userId);
		//RemoteUserProvider userMgr = new RemoteUserProvider();
		Person user = null;
		if (userId != null) 
		    user = CoreServiceLocator.getPersonService().findPersonById(new Long(userId));
		
		if (user == null) {
			log.error("VectricsException finding user, userId not found.  UserId = " + userId);
			throw new SystemException("VectricsException finding user, userId not found.  UserId = " + userId);
		}
		
		
		ContactInfo contactInfo = user.createContactInfoIfNotExist();
		log.debug("userId = " + userId);
		
		
		MarshalUtils marshaller = MarshalUtils.getInstance();
		marshaller.objectToElement(dataElement, user); //UserMarshalling.userToElement(user);
		
		/*
		NodeList elements = dataElement.getElementsByTagName("user");
		if (elements.getLength() > 0) {
			Element userElement = (Element)elements.item(0);
			MarshalUtils.objectToElement(userElement, user.getRoles(), "roles");
		}
		*/
	}
	


}
